package questions;

import java.util.List;

public class reverseList {
    class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }

    }

    public ListNode head;


    public void creatList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;

        this.head = node1;
    }


    public void diaplay() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }


    //反转链表
    //使用头插即可
    public ListNode reverseList() {
        //1. 防止空指针异常
        if (head == null) {
            return head;
        }

        //2. 将head.next置为空
        //注意先把 cur 节点给先弄出来
        ListNode cur = head.next;
        head.next = null;


        //3. 进行循环头插
        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = head;
            head = cur;
            cur = curN;
        }
        return head;
    }


    public ListNode middleNode1() {
        int count = 0;
        ListNode node = head;
        while (node != null) {
            count++;
            node = node.next;
        }
        ListNode cur = head;
        for (int i = 0; i < count / 2; i++) {
            cur = cur.next;
        }
        return cur;
    }

    public ListNode middleNode2() {
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }


    public int kthToLast(int k) {
        if (k <= 0) {
            return -1;
        }
        ListNode fast = head;
        ListNode slow = head;
        int count = 0;
        while (count != k - 1) {
            if (fast == null) {
                return -1;
            }
            fast = fast.next;
            count++;
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow.val;
    }


    public ListNode mergeTwoLists(ListNode headA, ListNode headB) {
        ListNode newH = new ListNode(0);
        ListNode tmp = newH;
        while (headA != null && headB != null) {
            if (headA.val < headB.val) {
                tmp.next = headA;
                headA = headA.next;
                tmp = tmp.next;
            } else if (headA.val > headB.val) {
                tmp.next = headB;
                headB = headB.next;
                tmp = tmp.next;
            }
        }
        if (headA == null) {
            tmp.next = headB;
        }
        if (headB == null) {
            tmp.next = headA;
        }
        return newH.next;
    }

    public ListNode partition(ListNode pHead, int x) {
        if (pHead == null) {
            return null;
        }
        // write code here
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;

        ListNode cur = pHead;
        while (cur != null) {
            if (cur.val < x) {
                if (bs == null) {
                    bs = be = cur;
                } else {
                    be.next = cur;
                    be = be.next;
                }
            } else {
                if (as == null) {
                    as = ae = cur;
                } else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if (bs == null) {
            return as;
        }
        be.next = as;
        if (as != null) {
            ae.next = null;
        }

        return bs;
    }

    public boolean chkPalindrome(ListNode head) {
        // write code here
        if (head == null) {
            return true;
        }

        //找到链表的中间节点
        ListNode fast = head;
        ListNode slow = head;

        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }

        //反转后半节点
        ListNode cur = slow.next;
        slow.next = null;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curN;
        }

        //slow从后往前，head从前往后，直到相遇
        while (head != slow) {
            if (head.val != slow.val) {
                return false;
            }
            if (head.next == slow)
                return true;
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        //1. 计算两个链表的长度，并计算差值
        int lenA = 0;
        int lenB = 0;
        ListNode curA = headA;
        ListNode curB = headB;
        while (curA != null) {
            curA = curA.next;
            lenA++;
        }
        while (curB != null) {
            curB = curB.next;
            lenB++;
        }
        int len = lenA - lenB;

        curA = headA;
        curB = headB;

        //2. 根据差值，长的链表头节点先走len步，
        // 让两个链表在同一起跑线
        if (len > 0) {
            int count = 0;
            while (count != len) {
                curA = curA.next;
                count++;
            }
        } else {
            int count = 0;
            while (count != -len) {
                curB = curB.next;
                count++;
            }
        }

        //3. 两个链表的节点携手同行，直到相等
        while (curA != curB) {
            curA = curA.next;
            curB = curB.next;
        }
        if (curA == null) {
            //若两个引用都为空，证明不相交
            return null;
        }
        return curA;
    }

    public boolean hasCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow)
                return true;
        }
        return false;
    }

    public ListNode detectCycle(ListNode head) {
        //判断是否有环
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            //有环，跳出循环
            if (fast == slow) {
                break;
            }
        }
        //若是因为没有环而跳出循环的话，返回null
        if (fast == null || fast.next == null) {
            return null;
        }
        //此时slow在相遇点，将fast拿到起始点
        //让他们相向而行，相遇点即为入口点
        fast = head;
        while(fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }

}


